package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnScenicEvaluateLikeBean;
import com.example.zhunongzonghe.dao.ZnScenicEvaluateLikeMapper;
import com.example.zhunongzonghe.dao.ZnUsersMapper;
import com.example.zhunongzonghe.entity.ZnScenicEvaluateLike;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.enums.ScenicEvaluateStatusEnum;
import com.example.zhunongzonghe.service.ZnScenicEvaluateLikeService;
import com.example.zhunongzonghe.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
* @author 蔡某人
* @description 针对表【zn_scenic_evaluate_like】的数据库操作Service实现
* @createDate 2024-04-25 17:28:35
*/
@Service
public class ZnScenicEvaluateLikeServiceImpl extends ServiceImpl<ZnScenicEvaluateLikeMapper, ZnScenicEvaluateLike>
    implements ZnScenicEvaluateLikeService{
    @Resource
    private RedisCache redisCache;
    @Autowired
    ZnUsersMapper znUsersMapper;
    private static final String EVALUATE_LIKE_KEY = "evaluate_like";//hash前缀
    private static final String EVALUATE_LIKE_COUNT_KEY = "evaluate_like_count";//hash前缀
    private static final String EVALUATE_LIKE_DETAIL_KEY = "evaluate_like_detail";//hash前缀

    @Override
    public void addLike(ZnScenicEvaluateLikeBean evaluateLikeBean, String userName) {
        LambdaQueryWrapper<ZnUsers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnUsers::getUserName,userName);
        ZnUsers znUsers = znUsersMapper.selectOne(queryWrapper);
        Integer evaluateId = evaluateLikeBean.getEvaluateId();
        Integer userId = znUsers.getId();
        Integer status = evaluateLikeBean.getLikeStatus();
        //与redis做交互的Hash的key
        String likeHashKey = buildPostLikeKey(evaluateId.toString(),userId.toString());
        redisCache.setCacheHashValue(EVALUATE_LIKE_KEY,likeHashKey,status);
        String detailKey = EVALUATE_LIKE_DETAIL_KEY+"."+evaluateId+"."+userId;//被谁点过赞的key
        String countKey = EVALUATE_LIKE_COUNT_KEY+"."+evaluateId;
        if (ScenicEvaluateStatusEnum.LIKED.getCode() == status){
            redisCache.increment(countKey,1);//点赞时加1
            redisCache.setCacheObject(detailKey,"1");
        }else {
            Integer count =redisCache.getInt(countKey);
            if (Objects.isNull(count) || count<0){
                return;
            }
            redisCache.increment(countKey,-1);//取消点赞时-1
            redisCache.deleteObject(detailKey);
        }
    }

    @Override
    public Boolean isLiked(Integer evaluateId, Integer userId) {
        String detailKey = EVALUATE_LIKE_DETAIL_KEY+"."+evaluateId+"."+userId;//被谁点过赞的key
        return redisCache.exist(detailKey);//获取redis内的detailKey，如果存在说明当前用户点赞过该评价
    }

    @Override
    public Integer likeCounts(Integer evaluateId) {
        String countKey = EVALUATE_LIKE_COUNT_KEY+"."+evaluateId;
        Integer count =redisCache.getInt(countKey);
        if (Objects.isNull(count) || count<0){
            return 0;
        }
        return redisCache.getInt(countKey);
    }

    private String buildPostLikeKey(String evaluateId,String userId){
        return evaluateId + ":" +userId;
    }
}




